<?php
namespace app\admin\controller;



use think\facade\View;
use think\Request;

class UserOauth extends Base
{
    protected $ModelPath = 'UserOauth';
    protected $model = null;
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->con = "BaUserOauthnk";
        $this->title = "授权";

        $module = $this->ModelPath;
        $this->model = new \app\admin\model\UserOauth();

        View::assign(array(
            'module' => $module,
            'title' => $this->title,
            'con' => $this->con,
            'pageSize' => $this->pageSize
        ));
    }

    public function index()
    {
        if(request()->isPost()){
            $params = request()->param();
            $map = [];

            if(!empty($params['id'])){
                $map[] = ['id', '=', $params['id']];
            }
            if(!empty($params['user_id'])){
                $map[] = ['user_id', '=', $params['user_id']];
            }
            if(!empty($params['oauth_id'])){
                $map[] = ['oauth_id', '=', $params['oauth_id']];
            }
            if(!empty($params['unionid'])){
                $map[] = ['id', '=', $params['unionid']];
            }

            $total = \app\admin\model\UserOauth::where($map)->count();

            $pageIndex = max(input('page'), 1);
            $pageSize = $this->pageSize;
            $p = ($pageIndex - 1) * $pageSize;

            $list = $this->model
                ->where($map)
                // ->where('rec',1)

                ->order('id DESC')
//                ->append(['status_format'])
                ->limit($p, $pageSize)
                //->fetchSql()
                ->select();
 //var_dump($list);die;
            $arr['data'] = $list;
            $arr['code'] = 0;
            // var_dump(1);die;
            $arr['count'] = $total;
            $arr['limit'] = $pageSize;
            $arr['msg'] = "获取" . $this->title . "列表成功";
            return json($arr);
        }else{
            return View::fetch();
        }
    }

    public function add(){
        if(request()->isPost()){
            $code = input('code');
            $codeArr = explode(',', $code);
            $sum = $codeArr[0] + $codeArr[1] + $codeArr[2] + $codeArr[3];
            $num = input('num');
            $add = [
                'time'         =>  input('time'),
                'num'     =>  $num,
                'code'     =>  $code,
                'sum'     =>  $sum,
                'status'     =>  1,
            ];

//            var_dump($add);die;

            $this->M->data($add);
            $result = $this->M->save();
            if ($result !== false) {
                (new \app\model\Num())->insert([
                    'name'=>(int)$num + 1,
                    'type'=>1,
                ]);
                $arr['status'] = 1;
                $arr['msg'] = '添加成功';
            } else {
                $arr['status'] = 0;
                $arr['msg'] = '添加失败';
            }
            return $arr;
        }else{
//            $numArr = Db::name('num')
//                ->where('type', 1)
//                ->order('id DESC')->select();
//            View::assign(array(
//                'numArr'        =>    $numArr,
//            ));
            return View::fetch();
        }
    }

    public function edit(){
        if(request()->isPost()){
            $params = request()->param();
            $update = $params;

            $save = $this->model->update($update);
            if ($save) {
                $arr['status'] = 1;
                $arr['msg'] = '修改成功';
            } else {
                $arr['status'] = 0;
                $arr['msg'] = '修改失败';
            }
            return $arr;
        }else{
            $id = input('id');
            $info = $this->model->where('id', $id)->find();
            View::assign(array(
                'info'        =>    $info,
            ));
            return View::fetch();
        }
    }

    public function merchant_order()
    {
//        var_dump($this->M->toArray());die;
        if(request()->isPost()){
            $params = request()->param();
            $map = [];

            $orderList = MerchantOrder::where('merchant_id', $this->merchantId)
                ->where('status', 'payed');
//            if(isset($params['title'])){//return 1;
//                // $title = input('title');
//                $this->model = $this->model->where("title", "like", $params['title']);
//            }
//            if(isset($params['author_id'])){
//                // $authorId = input('author_id');var_dump(1);die;
//                $this->model = $this->model->where("author", "like", $params['author_id']);
//            }

//             if(input('tag_id')){
//                 $authorId = input('tag_id');
// //                $map['_string'] = 'FIND_IN_SET(' . $authorId . ', tag_list)';
//                 $map[] = ['exp', Db::raw("FIND_IN_SET(" . $authorId . ", tag_list)")];
//             }

            $list = $orderList
                // ->where($map)
                ->select();

            $total = count($list);

            $pageIndex = max(input('page'), 1);
            $pageSize = $this->pageSize;
            $p = ($pageIndex - 1) * $pageSize;

            $list = $orderList
                // ->where($map)
                // ->where('rec',1)

                ->order('id DESC')
                ->append(['type_format'])
                ->limit($p, $pageSize)
                ->select();
// var_dump($list);die;
            $arr['data'] = $list;
            $arr['code'] = 0;
            // var_dump(1);die;
            $arr['count'] = $total;
            $arr['limit'] = $pageSize;
            $arr['msg'] = "获取" . $this->title . "列表成功";
            return json($arr);
        }else{
            return View::fetch();
        }
    }

    public function order()
    {
//        var_dump($this->M->toArray());die;
        if(request()->isPost()){
            $params = request()->param();
            $map = [];

            $orderList = Order::where('merchant_id', $this->merchantId)
                ->where('status', 'payed');
//            if(isset($params['title'])){//return 1;
//                // $title = input('title');
//                $this->model = $this->model->where("title", "like", $params['title']);
//            }
//            if(isset($params['author_id'])){
//                // $authorId = input('author_id');var_dump(1);die;
//                $this->model = $this->model->where("author", "like", $params['author_id']);
//            }

//             if(input('tag_id')){
//                 $authorId = input('tag_id');
// //                $map['_string'] = 'FIND_IN_SET(' . $authorId . ', tag_list)';
//                 $map[] = ['exp', Db::raw("FIND_IN_SET(" . $authorId . ", tag_list)")];
//             }

            $list = $orderList
                // ->where($map)
                ->select();

            $total = count($list);

            $pageIndex = max(input('page'), 1);
            $pageSize = $this->pageSize;
            $p = ($pageIndex - 1) * $pageSize;

            $list = $orderList
                // ->where($map)
                // ->where('rec',1)

                ->order('id DESC')
                ->append(['type_format'])
                ->limit($p, $pageSize)
                ->select();
// var_dump($list);die;
            $arr['data'] = $list;
            $arr['code'] = 0;
            // var_dump(1);die;
            $arr['count'] = $total;
            $arr['limit'] = $pageSize;
            $arr['msg'] = "获取" . $this->title . "列表成功";
            return json($arr);
        }else{
            return View::fetch();
        }
    }

    //所有用户开户
    public function allUserOpen()
    {
//        $userBank = \app\admin\model\UserBank::where('user_id', 64)->find();

        $userBankList = \app\admin\model\UserBank::where('status', 'pending')->select();

        foreach ($userBankList as $userBank){
            $res = (new \app\admin\model\Setplus())->userOpen($userBank);
            if($res['respCode'] == '00'){
                if($res['status'] == 3){
                    \app\admin\model\UserBank::where('id', $userBank['id'])->update([
                        'status'=>'enabled'
                    ]);
                }
            }else{
                if($res['respCode'] == '94'){
                    \app\admin\model\UserBank::where('id', $userBank['id'])->update([
                        'status'=>'enabled'
                    ]);
                }else{
                    \app\admin\model\UserBank::where('id', $userBank['id'])->update([
                        'msg'=>$res['message']
                    ]);
                }
            }
        }
    }

    public function test()
    {
//        (new \app\admin\model\Setplus())->userOpen();
        (new \app\admin\model\Setplus())->withDraw();
    }
}